package com.example.excel.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.example.excel.entity.ForecastElectricityRevenue;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author dingfeng
 * @version 1.0
 * @DEC
 * @date 2022-03-31 16:48
 */
public class ExcelModelListener extends AnalysisEventListener<ForecastElectricityRevenue> {
    private JdbcTemplate jdbcTemplate;

    private Date uploadTime;

    private List dataList;

    private static final String SQL= "insert into dm_zj_fj.forecast_electricity_revenue values(?,?,?)";

    public ExcelModelListener(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
        dataList = new ArrayList();
        uploadTime = new Date();
    }

    @Override
    public void invoke(ForecastElectricityRevenue data, AnalysisContext context) {
        System.out.println(data);
        Object[] item =  new Object[3];
        item[0] = data.getRq();
        item[1] = data.getIncome();
        item[2] = uploadTime;
        dataList.add(item);
        System.out.println(data);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
       //数据入库
        jdbcTemplate.batchUpdate(SQL,dataList);

    }


}
